<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class SysAdminController extends CommonController {

    protected $_modelName = "SysAdmin";

    public function index() {
        $this->display();
    }

    protected function setCode() {
        $this->_code = "System";
    }

/////////////////管理员/////////////////////////////////
    public function saveAdmin() {
        $this->_addAdmin();
    }
    
    /**
     *  添加管理员帐号
     */
    private function _addAdmin() {
        $this->checkPriv('sysAdmin', 2);

        $this->_setModel();
        $rt = $this->_model->addAdmin();

        if ($rt) {
            $this->ajaxReturn(array('result' => 1));
        } else {
            $this->ajaxReturn(array('result' => 0, 'desc' => $this->_model->getError() . $this->_model->getDbError()));
        }
    }

    /**
     * 修改本人密码
     */
    public function updatePsw() {
		$oldPassword = I("post.oldPassword");
		$rePassword = I("post.rePassword");
		$password = I("post.password");
        if (empty($oldPassword) || empty($rePassword) || empty($password)) {
            $this->ajaxReturn(array('result' => 0, 'desc' => '部分必要数据为空，请检查后重新提交'));
        }
        $this->_setFields(I("session.adminid"));
    }

    /**
     * 重置指定管理员密码
     */
    public function resetPsw() {
        $this->checkPriv('sysAdmin', 2);
		$rePassword = I("post.rePassword");
		$password = I("post.password");
        if (empty($rePassword) || empty($password)) {
            $this->ajaxReturn(array('result' => 0, 'desc' => '部分必要数据为空，请检查后重新提交'));
            exit();
        }
        //判断该管理员是否为超级管理员，是的话不允许修改
        $this->_isSuperAdmin(I("post.adminID"));
        $this->_setFields(I("post.adminID"));
    }

    /**
     *  修改管理员角色
     */
    public function updateAdminRole() {
        $this->checkPriv('sysAdmin', 2);

        //判断该管理员是否为超级管理员，是的话不允许修改
        $this->_isSuperAdmin(I("post.adminID"));
        $this->_setOneFieldByID(I("post.adminID"), 'roleID', I("post.roleID"));
    }

    /**
     *  删除管理员
     */
    public function deleteAdmin() {
        $this->checkPriv('sysAdmin', 2);

        //判断该管理员是否为超级管理员，是的话不允许修改
        $this->_isSuperAdmin(I("post.adminID"));

        $admin = $this->_model;
        $rt = $this->_model->deleteAdmin(I("post.adminID"));

        $rejson = $rt ? array('result' => 1) : array('result' => 0, 'desc' => $admin->getError() . $admin->getDbError());

        $this->ajaxReturn($rejson);
    }

    /**
     * 得到条件查询数组
     * @return Array
     */
    protected function _getQueryArr(&$where, &$order) {
        $where = array();
        if (strlen(trim(I("post.sAccount"))) > 0) {
            $where['account'] = array("like", "%" . trim(I("post.sAccount") . "%"));
        }
        if (strlen(trim(I("post.sNickname"))) > 0) {
            $where['nickname'] = array("like", "%" . trim(I("post.sNickname") . "%"));
        }
        if (strlen(trim(I("post.sEmail"))) > 0) {
            $where['email'] = array("like", "%" . trim(I("post.sEmail") . "%"));
        }
        if (strlen(trim(I("post.sPhone"))) > 0) {
            $where['phone'] = I("post.sPhone");
        }

        $where['manType'] = 0;
        $where['isOn'] = 1;

        $orderBy = 'addTime';
        $desc = 'desc';
        if (intval(I("post.sDesc")) === 1) {
            $desc = 'asc';
        }
        if (strlen(trim(I("post.sOrderBy", ''))) > 0) {
            $orderBy = I("post.sOrderBy");
        }

        $order = array(
            $orderBy => $desc
        );
    }

    // 查看管理员列表（超级管理员帐号除外）
    public function adminList() {
//        dump($_SESSION);
        $this->checkPriv('sysAdmin', 1, false);

        $this->assign("roleList", D("SysAdminRole")->where('isOn = 1')->select());
        $this->_getOnePageData("getAdminList", "getAdminListLength");
    }

    /**
     * 查看管理员信息
     */
    public function adminMsg() {
        $this->checkPriv('sysAdmin', 1, false);

        $adminID = intval(I("adminID", 0));

        $this->_setModel();
        $arr = $this->_model->getAdmin($adminID);
        if($arr) {
            $rejson['result'] = 1;
            $rejson['arr'] = $arr;
        } else {
            $rejson['result'] = 0;
            $rejson['desc'] = $this->_model->getError() . $this->_model->getDbError();
        }
        
        $this->ajaxReturn($rejson);
        
    }

    /**
     * 更新本人基本信息(除了密码)
     */
    public function updateMsg() {
        $this->_setFields(I("session.adminID"));
    }

    /**
     * 判断是否为超级管理员
     * @param type $adminID
     * @return type
     */
    private function _isSuperAdmin($adminID) {
        $this->_setModel();
        $manType = $this->_model->getOneField(array("adminID" => $adminID), "manType");
        if (intval($manType) === 1) {
            $this->ajaxReturn(array('result' => 0, 'desc' => '不允许修改超级管理员信息'));
        }
    }

    /**
     * 更新信息(除了密码)
     * @param type $adminID
     */
    private function _setFields($adminID) {
        if ($adminID <= 0) {
            $this->ajaxReturn(array('result' => 0, 'desc' => '无效ID'));
        }

        $rt = false;
        $this->_setModel();
        $_POST['adminID'] = $adminID;

        $rt = $this->_model->updateMsg();

        $rejson = $rt ? array('result' => 1) : array('result' => 0, 'desc' => $this->_model->getError() . $this->_model->getDbError());

        $this->ajaxReturn($rejson);
    }

    /**
     * 根据id更新一个属性（除密码）
     * @param type $adminID
     * @param type $key
     * @param type $val
     */
    private function _setOneFieldByID($adminID, $key, $val) {
        $this->_setModel();
        $admin = $this->_model;
        $rt = $admin->updateOneFieldById($adminID, $key, $val);

        $rejson = $rt ? array('result' => 1) : array('result' => 0, 'desc' => $admin->getError() . $admin->getDbError());

        $this->ajaxReturn($rejson);
    }

}
